package com.zelin.controller;

import com.zelin.pojo.Product;
import com.zelin.pojo.Result;
import com.zelin.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WF on 2020/10/29 10:54
 */
@RestController
@RequestMapping("product")
@Slf4j
@RefreshScope           //这里要加上此注解，修改配置才不需要重启
public class ProductController {

    @Autowired
    private ProductService productService;
    @Value("${server.port}")
    private int port;

    @Value("${config.info}")
    private String info;        //读取配置信息
    //1.根据id查询商品
    @GetMapping("{pid}")
    public Product findById(@PathVariable Integer pid){
        System.out.println("port:" + port);
        log.info("读取的配置文件内容：" + info);
        return productService.findById(pid);
    }

    //2.添加商品
    @PostMapping
    public Result add(@RequestBody  Product product){
        productService.add(product);
        return new Result(true,"添加商品成功！");
    }

    //3.查询所有商品
    @GetMapping
    public List<Product> findAll(){
        return productService.findAll();
    }

}
